package com.welsy.classroom.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.welsy.classroom.event.GetClassComponentsEvent;
	import com.welsy.classroom.business.GetClassComponentsDelegate;
	import com.welsy.usermgt.vo.ClassVO;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.usermgt.vo.UserVO;
	import com.welsy.classroom.model.ClassModelLocator;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.classroom.event.GetAllEditingClassroomEvent;
	import com.welsy.classroom.business.GetAllEditingClassroomDelegate;
	import mx.effects.AnimateProperty;
	import mx.collections.ArrayCollection;
	import com.welsy.stats.model.StatsModelLocator;
	
	public class GetAllEditingClassroomCommand implements Command, IResponder
	{
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetAllEditingClassroomDelegate = new GetAllEditingClassroomDelegate(this);
			var getAllEditingClassroomEvent : GetAllEditingClassroomEvent = GetAllEditingClassroomEvent(event);
			
			delegate.getEditingClassroom(getAllEditingClassroomEvent.authorId);
		}
		
		public function result(event : Object) :void {
			//parse the server result
			var aNS : Namespace = new Namespace(NamespaceLocator.USER_MGT_DATATYPES);
			var bNS : Namespace = new Namespace(NamespaceLocator.CLASSROOM_DATATYPES);
			var base : XMLList = XMLList(event.result);
			
			var TAG_CLASSROOMCOLLECTION : QName = new QName(bNS, "ClassroomsCollection");
			
			var TAG_CLASSROOMTYPE : QName = new QName(bNS, "ClassroomType");

			var TAG_CLASSROOMID : QName = new QName(aNS, "ClassroomId");
			var TAG_TITLE : QName = new QName(aNS, "Title");
			var TAG_DESCRIPTION : QName = new QName(aNS, "Description");
			var TAG_FROMDATE : QName = new QName(aNS, "FromDate");
			var TAG_TODATE : QName = new QName(aNS, "ToDate")
			var TAG_AUTHORID : QName = new QName(aNS, "AuthorId");
			var TAG_CREATIONDATE : QName = new QName(aNS, "CreationDate");
			var TAG_NOTE : QName = new QName(aNS, "Note");
			
			//add the users
			var classes : XMLList = base.descendants(TAG_CLASSROOMCOLLECTION).children();
			var classList : ArrayCollection = new ArrayCollection;
			//classList.removeAll();
			for each(var classT : XML in classes) {
				var buffer : ClassVO = new ClassVO;
				buffer.classId = classT.child(TAG_CLASSROOMID);
				buffer.className = classT.child(TAG_TITLE);
				buffer.authorId= classT.child(TAG_AUTHORID);
				buffer.classDescr = classT.child(TAG_DESCRIPTION);
				buffer.creationDate = ClassVO.stringToDate(classT.child(TAG_CREATIONDATE));
				buffer.durationFrom = ClassVO.stringToDate(classT.child(TAG_FROMDATE));
				buffer.durationTo = ClassVO.stringToDate(classT.child(TAG_TODATE));
				buffer.classNotes = classT.child(TAG_NOTE);
				
				classList.addItem(buffer);
			}
			ClassModelLocator.getInstance().classes = classList;
		}
		
		public function fault( event : Object ):void {
			//TODO: report the error
			Alert.show("GetAllEditingCommand.fault!");
		}
		
	}
}